/*
 * @Description: 
 * @Author: Liu Kunpeng
 * @Date: 2021-07-12 21:21:43
 */
import store from './store/store'
import router, {
    endRoute,
    footRoute,
    resetRouter,
    threeRoute
} from "./router";
import {
    getToken,
    removeToken
} from '@/utils/auth'
//挂载路由导航守卫
router.beforeEach(async(to, from, next) => {
    // to 将要访问
    //from 从哪访问
    //next 接着干 next(url) 重定向到url上next()继续访问 to路径
    
    if (to.path === "/login" || to.path === "/") {
        resetRouter()
        next()
    } else {
        const token = getToken()
        if (!token) {
            next('/')
        }
        next()
        if (store.state.userinfo.type === undefined) {
            await store.dispatch('getUserInfo')
        }
        const userType = store.state.userinfo.type
        var addRoutes
        // console.log(userType);
        if (userType === "步态分析") {
            addRoutes = [...footRoute, ...endRoute]
        } else if (userType === "三维扫描") {
            addRoutes = [...threeRoute, ...endRoute]
        } else {
            addRoutes = [...endRoute]
        }
        // router.addRoutes(addRoutes)  //Vue 4 已经废弃addRoutes方法
        addRoutes.forEach(item => {
            router.addRoute(item);
        });
        router.options.routes = router.options.routes.concat(addRoutes)
    }

})